# -*- coding: utf-8 -*-
# author: sixmillions
# date: 2022/04/18


import os
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def send_mail(subject):
    email_host = 'smtp.163.com'  # 服务器地址 163邮箱"smtp.163.com"  qq邮箱"smtp.qq.com"都需要开通smtp权限
    sender = 'liubw95@163.com'  # 发件人（自己的邮箱）
    password = os.environ.get('MAIL_AUTH_CODE')  # 密码（自己邮箱的登录密码）
    receiver = 'liubw95@163.com'  # 收件人

    msg = MIMEMultipart()
    msg['Subject'] = subject  # 标题
    msg['From'] = 'liubw95@163.com'  # 邮件中显示的发件人别称
    msg['To'] = receiver  # ...收件人...

    mail_msg = '''
    <p>自动化脚本发送!</p>
    <p>无需回复</p>
    <p>截图如下：</p>
    <p><img src="cid:image1"></p>
    '''
    msg.attach(MIMEText(mail_msg, 'html', 'utf-8'))
    # 指定图片为当前目录
    fp = open(r'/opt/packages/mrjk.png', 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    # 定义图片 ID，在 HTML 文本中引用
    img.add_header('Content-ID', '<image1>')
    msg.attach(img)

    smtp = smtplib.SMTP_SSL(email_host, 994)
    smtp.login(sender, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()
    print('success')
